package com.authorization.demo.service.impl;
import com.authorization.demo.entity.Menu;
import com.authorization.demo.entity.RoleTable;
import com.authorization.demo.entity.UserRole;
import com.authorization.demo.mapper.MenuMapper;
import com.authorization.demo.mapper.RoleTableMapper;
import com.authorization.demo.service.MenuRoleService;
import com.authorization.demo.service.MenuService;
import com.authorization.demo.service.UserRoleService;
import com.authorization.demo.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author 梁陈意
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Resource
    private MenuMapper menuMapper;
    @Resource
    private UserRoleService userRoleService;
    @Resource
    private MenuRoleService menuRoleService;
    @Resource
    private RoleTableMapper roleTableMapper;
    //通过菜单名来获取菜单类型
    @Override
    public int menuType(String menuName) {
        Menu menu = menuMapper.selectById(menuName);
        return menu.getType();
    }

    //通过菜单名来查找获取其子菜单的实体列表
    @Override
    public List<Menu> findSubmenu(String menuName){
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PREVIOUS",menuName);
        queryWrapper.eq("IS_DELETE",1);
        List<Menu> subMenulist = menuMapper.selectList(queryWrapper);
        return subMenulist;
    }

    //通过菜单名来查找获取其子菜菜单名列表


    @Override
    public List<String> findSubmenuName(String menuName) {
         List<Menu> subMenulist = this.findSubmenu(menuName);
         List<String> subMenunamelist = new ArrayList<>();
         for(Menu menu:subMenulist){
             subMenunamelist.add(menu.getMenuName());
         }
         return subMenunamelist;
    }

    //传入菜单名，逻辑删除当前菜单，成功则返回true
    @Override
    public boolean delCurrentmenu(String menuName) {
        Menu menu = menuMapper.selectById(menuName);
        menu.setIsDelete(0);
        if(this.updateById(menu)) {
            return true;
        }
        return false;
    }

    @Override
    public R delmenu(String menuName) {
        //逻辑删除当前菜单
        if(!this.delCurrentmenu(menuName)){
            return R.error().setMessage("逻辑删除传入的菜单出现问题");
        }
        //传入的菜单类型为三级菜单则不需要考虑其子菜单的情况
        if(this.menuType(menuName)==3) {
            return R.ok();
        }
        if(this.menuType(menuName)==1){
            List<Menu> subMenulist = this.findSubmenu(menuName);
            //考虑一级菜单没有子菜单的情况
            if(subMenulist == null || subMenulist.size() ==0 ){
                return R.ok();
            }
            //一级菜单有子菜单的情况
            else{
                for (Menu menu:subMenulist) {
                    if(!this.delCurrentmenu(menu.getMenuName())){
                        return R.error().setMessage("逻辑删除传入的菜单出现问题");
                    }
                    //逻辑删除一级菜单的子菜单下的子菜单
                    if(menu.getType()==2){
                        List<Menu> subsubMenulist = this.findSubmenu(menu.getMenuName());
                        for (Menu submenu:subsubMenulist) {
                            if(!this.delCurrentmenu(submenu.getMenuName())){
                                return R.error().setMessage("逻辑删除传入的菜单出现问题");
                            }
                        }
                    }
                }
            }
            return R.ok();
        }
        if(this.menuType(menuName)==2){
            List<Menu> subMenulist = this.findSubmenu(menuName);
            //考虑二级菜单没有子菜单的情况
            if(subMenulist == null || subMenulist.size() ==0 ){
                return R.ok();
            }
            //二级菜单有子菜单的情况
            else{
                for (Menu menu:subMenulist) {
                    if(!this.delCurrentmenu(menu.getMenuName())){
                        return R.error().setMessage("逻辑删除传入的菜单出现问题");
                    }
                }
                return R.ok();
            }
        }
        return R.error().setMessage("菜单类型出错啦");
    }

    @Override
    public boolean findMenuexist(String menuName) {
        if (menuMapper.selectById(menuName) != null) {
            return true;
        }
        return false;
    }

    @Override
    public List<String> findUsermenu(String userName) {
        List<String> roleNameList = new ArrayList<>();
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_NAME", userName);
//        queryWrapper.eq("STATUS", 1);
        List<UserRole> userRoles = userRoleService.list(queryWrapper);
        Iterator<UserRole> iterator = userRoles.iterator();
        while (iterator.hasNext()) {
            UserRole userRole = iterator.next();
            String roleName= userRole.getRoleName();
            //筛选出角色状态为1的有效角色
            if(roleTableMapper.selectById(roleName).getStatus()==1){
                roleNameList.add(roleName);
            }
        }
        //根据userName返回该用户所具有的角色列表roleNameList
        List<String> menuList = new ArrayList<>();
        for(String roleName : roleNameList){
            List<String> menuNameList = menuRoleService.GetMenuNameListByRoleName(roleName);
            menuList.addAll(menuNameList);
        }
        //对menuList进行去重
        List<String> userMenuList = menuList.stream().distinct().collect(Collectors.toList());
        return userMenuList;
    }
}
